package gui;

import java.awt.Color;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class TableModel extends DefaultTableModel
{
	public int row = -1;/* initial value -1 by MSalah 20080925_AM1030
						 to force user to select a row and setting row to the right value
						 and we can check against -1 to make sure.
						*/
	public int col;

	private boolean[] cellEditable = null;
	
	public void initTableModel()
	{
		this.cellEditable = new boolean[this.getColumnCount()];
		for(int i = 0 ; i < this.getColumnCount() ; i++)
		{
			this.setCellEditable(0,i, false);
		}
	}
	
	public void addRow(String[] data, Color c)
	{		
		int rowIndex = this.getRowCount();
		this.setRowCount(this.getRowCount()+1);
		for(int i=0;i<data.length;i++)
		{
			this.setValueAt(new ColoredText(data[i],c, Color.white), rowIndex, i);
		}
	}
	
	public void setRowColor(int row, Color textColor, Color backGround)
	{
		for(int i=0;i<this.getColumnCount();i++)
		{
			ColoredText coloredText = (ColoredText)(getValueAt(row, i));
			coloredText.setColor(textColor);
			coloredText.backGround = backGround;
		}
	}
	public void setCelColor(int row, int col, Color clr, Color bckGrnd)
	{
		ColoredText coloredText = (ColoredText)(getValueAt(row, col));
		coloredText.setColor(clr);
		coloredText.backGround = bckGrnd;
	}
	

	public void setCellEditable(int row, int column, boolean value)
	{
		this.cellEditable[column] = value;
	}

	public boolean isCellEditable(int row, int column)
	{
		return cellEditable[column];
	}

	public void clearTableModel()
	{
		for ( int i = 0 ; i < this.getRowCount() ; i++)
		{
			for (int j = 0 ; j < this.getColumnCount(); j++)
			{
				setValueAt("", i, j);
			}
		}
	}
	public void deleteTableRows()
	{
		this.getDataVector().removeAllElements();
	}

	public int getCol()
	{
		return col;
	}

	public void setCol(int col)
	{
		this.col = col;
	}

	public int getRow()
	{
		return row;
	}

	public void setRow(int row)
	{
		this.row = row;
	}
	public void dumpData()
	{
		String str = "";
		str += "============================================================================";
		for(int i =0 ; i < this.getRowCount(); i++)
		{
			str += "\n";
			for(int j=0; j < this.getColumnCount() ; j++)
			{
				str += this.getValueAt(i, j) + "\t";
			}
		}
		str += "\n";
		str += "=============================================================================";
		System.out.println(str);
		
	}
}
